package com.mikepenz.app.activity.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mikepenz.app.R;
import com.mikepenz.presenter.LoginPresenter;
import com.mikepenz.presenter.imp.LoginPresenterImp;
import com.mikepenz.view.LoginView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2015/9/14.
 */
public class LoginActivity extends BaseActivity implements LoginView {

    @InjectView(R.id.login_phone)
    EditText loginPhone;
    @InjectView(R.id.login_pwd)
    EditText loginPwd;
    @InjectView(R.id.login_submit)
    Button loginSubmit;

    LoginPresenter presenter;
    ProgressDialog loading;
//    @InjectView(R.id.login_forget)
//    TextView loginForget;
    @InjectView(R.id.login_regist)
    TextView loginRegist;
    @InjectView(R.id.login_remeber)
    CheckBox loginRemeber;
    String username,pwd;

    public void initListener() {
        loginPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                presenter.userNameCheck(loginPhone.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        loginPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                presenter.pwdCheck(loginPwd.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        initListener();
        loading = new AlertHelper(this).LoadingAlert();
        if (username!=null) {
            loginPhone.setText(username);
            loginPwd.setText(pwd);
        }
    }

    @Override
    public void initActionbar() {
    }

    @Override
    public void initData() {
        presenter = new LoginPresenterImp(this);
        SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        if (sharedPreferences.getString("username",null)!=null){
            username = sharedPreferences.getString("username",null);
            pwd = sharedPreferences.getString("pwd",null);
        }
    }

    @Override
    public void userNameOrPwdError(String error) {
        loginPhone.setError(error);
    }

    @Override
    public void showAlert(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        loading.show();
    }

    @Override
    public void dimissLoading() {
        loading.dismiss();
    }

    @Override
    public void userNameFailed(String error) {
        loginPhone.setError(error);
    }

    @Override
    public void pwdFailed(String error) {
        loginPwd.setError(error);
    }

    @Override
    public boolean remenber() {
        return loginRemeber.isChecked();
    }

    @OnClick(R.id.login_submit)
    protected void submit() {
        if (!presenter.userNameCheck(loginPhone.getText().toString())) return;
        if (!presenter.pwdCheck(loginPwd.getText().toString())) return;
        presenter.login(loginPhone.getText().toString(), loginPwd.getText().toString());
    }
    @OnClick(R.id.login_regist)
    protected void regist(){
        Intent intent = new Intent(this,RegistActivity.class);
        startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
